import ast
import dis

source_code = """
a=1
b=2
print(a+b)

def add(x,y):
    return a+b
"""


# 1.将源码进行词法分析，编程抽象语法树ast
try:
    code_tree = ast.parse(source_code)
    # print(ast.dump(code_tree, indent=2))
except SyntaxError as e:
    print("语法错误{}".format(e))


# 2.将ast编译为字节码对象

try:
    # 使用compile方法将源码的ast变成pvm(python虚拟机)能够识别的字节码对象
    byte_obj = compile(code_tree, "<string>", "exec")
    print(byte_obj)

    # 调用 dis.dis 反汇编字节码，输出详细的字节码指令列表
    dis.dis(byte_obj)

except SyntaxError as e:
    print(f"语法错误{e}")


# 导入模块operate
import operate

x = 100
y = 200
print(add(x, y))
